extends Node
signal progress
signal physics_progress

var tscnEnemy=preload("res://prefab/enemy.tscn")
var tscnPlayer=preload("res://prefab/player.tscn")



func objGet(obj,key):
	if obj==null:
		return null
	if !obj.has(key):
		return null
	return null

#玩家等级属性 和需要的经验
func get_playerLvlData(lvl:int):
	var elvl=lvl-1
	var obj={
		'max_hp':int(240+elvl*24),
		'atk':int(18+elvl*1.8),
		'def':int(12+elvl*1.2),
		'next_experience':expForLevel(lvl+1)
	}
	return obj

#升到某个等级需要的总经验
func expForLevel(level):
	var basis=30.0 #用小数，除法是不会自动取整
	var extra=20.0
	var acc_a=30.0
	var acc_b=30.0
	return (round(
		(basis * pow(level - 1, 0.9 + acc_a / 250) * level * (level + 1)) /
			(6 + pow(level, 2) / 50 / acc_b) +
			(level - 1) * extra
	))

func createJumpNum(text,regionId,pos):
	var battlePlace=getBattlePlace(regionId)
	if !battlePlace:
		return
	var tscn=load('res://prefab/damageNum.tscn')
	var jumpNum=tscn.instance() as JumpNum
	jumpNum.position=pos
	battlePlace.add_child(jumpNum)
	jumpNum.setText(text)
	
	pass

var battlePlaceDic:Dictionary={}
func createEnemy(id,regionId:int,pos:Vector2):
	id=str(id)
	var battlePlace=getBattlePlace(regionId)
	if !battlePlace:
		return
	var actorM=createEnemyActorM(id)
	var enemy=tscnEnemy.instance()
	enemy.position=pos
	battlePlace.add_child(enemy)
	enemy.start(actorM)
func createPlayer(regionId:int,pos:Vector2):
	var battlePlace=getBattlePlace(regionId)
	if !battlePlace:
		return
	var m=ActorM.new()
	m.type='player'
	m.camp='player'
	var p=tscnPlayer.instance()
	p.position=pos
	battlePlace.add_child(p)
	p.start(m)
func getBattlePlace(regionId:int):
	return battlePlaceDic.get(regionId,null)
#生成acormM 怪物和npc用的，player用另外的逻辑
func createNPCActorM(id:String):
	var actorM=_createActorM(id,'npcs')
	actorM.type='npc'
	actorM.camp='player'
	return actorM
func createEnemyActorM(id:String):
	var actorM=_createActorM(id,'enemys')
	actorM.type='enemy'
	actorM.camp='enemy'
	return actorM
func _createActorM(id:String,sheetName:String):
	var datas=GDSheets.sheet(sheetName)
	var data=datas.get(id)
	var actorM=ActorM.new()
	if !data:
		data={}
	actorM.n_max_hp.w_base.value=data.get('max_hp',200)
	actorM.n_atk.w_base.value=data.get('atk',10)
	actorM.n_def.w_base.value=data.get('def',10)
	actorM.n_attackSpeed.w_base.value=data.get('attackSpeed',100)
	actorM.n_attackRange.w_base.value=data.get('attackRange',200)
	actorM.n_moveSpeed.w_base.value=data.get('moveSpeed',100)
	actorM.killExp=data.get('killExp',0)
	actorM.skin=data.get('skin','')
	return actorM

func _process(delta):
	emit_signal("progress",delta)
	pass
func _physics_process(delta):
	emit_signal("physics_progress",delta)
